/**
 *
 * @author pquiring
 */

import java.io.*;
import java.awt.*;
import javax.swing.*;

import javaforce.*;
import javaforce.gl.*;

public class ThreeDeeApp extends javax.swing.JFrame {

  /**
   * Creates new form ThreeDeeApp
   */
  public ThreeDeeApp() {
    initComponents();
    PanelTools.frame = this;
    initGL();
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    menubar = new javax.swing.JMenuBar();
    file = new javax.swing.JMenu();
    open = new javax.swing.JMenuItem();
    save = new javax.swing.JMenuItem();
    saveAs = new javax.swing.JMenuItem();
    edit = new javax.swing.JMenu();
    about = new javax.swing.JMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    file.setText("File");

    open.setText("Open");
    open.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        openActionPerformed(evt);
      }
    });
    file.add(open);

    save.setText("Save");
    file.add(save);

    saveAs.setText("SaveAs");
    file.add(saveAs);

    menubar.add(file);

    edit.setText("Edit");

    about.setText("About");
    edit.add(about);

    menubar.add(edit);

    setJMenuBar(menubar);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 460, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 359, Short.MAX_VALUE)
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void openActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openActionPerformed
    open();
  }//GEN-LAST:event_openActionPerformed

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new ThreeDeeApp().setVisible(true);
      }
    });
  }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JMenuItem about;
  private javax.swing.JMenu edit;
  private javax.swing.JMenu file;
  private javax.swing.JMenuBar menubar;
  private javax.swing.JMenuItem open;
  private javax.swing.JMenuItem save;
  private javax.swing.JMenuItem saveAs;
  // End of variables declaration//GEN-END:variables
  public boolean canvasInit;
  public JPanel panel;
  public static int minGLVersion = 2;  //increase to 3 if offscreen buffer is needed
  public void initGL() {
    panel = new JPanel();
    panel.setLayout(new BorderLayout());
    Data.canvas = new GLCanvas() {
      public void paint(Graphics g) {
        if (!canvasInit) {
          canvasInit = true;
          Data.canvas.init(new GLInterface() {
            public void init(GL gl, Component comp) {
              if (Data.scene != null) return;
              int version[] = gl.getVersion();
              if (version[0] < minGLVersion) {
                JF.showError("Error", "OpenGL " + minGLVersion + ".0 or higher required");
                System.exit(0);
              }
              Data.scene = new GLScene();
              Data.scene.init(gl, GLVertexShader.source, GLFragmentShader.source);
              initGUI();
            }

            public void render(GL gl) {
            }

            public void resize(GL gl, int i, int i1) {}
          });
        }
      }
    };
    panel.add(Data.canvas, BorderLayout.CENTER);
    setContentPane(panel);
    JF.centerWindow(this);
  }

  public void initGUI() {
    JSplitPane split = PanelTools.createSplit(PanelTools.HORIZONTAL);
    ViewPanel view = new ViewPanel();
    PanelTools.setPanel(split, view, "left");
    CreatePanel create = new CreatePanel();
    PanelTools.setPanel(split, create, "right");
    setContentPane(split);
    Dimension d = create.getPreferredSize();
    split.setDividerLocation(getWidth() - d.width);
    JF.centerWindow(this);
    revalidate();
    repaint();
  }

  public void open() {
    GLModel model = null;
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.setMultiSelectionEnabled(false);
    chooser.setCurrentDirectory(new File(JF.getCurrentPath()));
    if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) return;
    File file = chooser.getSelectedFile();
    int idx = file.toString().lastIndexOf(".");
    if (idx == -1) {
      JF.showError("Error", "File not supported");
      return;
    }
    String ext = file.toString().toLowerCase().substring(idx);
    if (ext.equals(".3ds")) {
      model = Data.scene.load3DS(file.getAbsolutePath());
    } else if (ext.equals(".blend")) {
      model = Data.scene.loadBlend(file.getAbsolutePath());
    } else if (ext.equals(".jf3d")) {
      model = Data.scene.loadJF3D(file.getAbsolutePath());
    } else {
      JF.showError("Error", "File not supported");
      return;
    }
    Data.scene.addModel(model);
    if (!Data.scene.loadTextures()) {
      JF.showError("Error", "Some textures were not found!");
    }
    Data.model = model;
    repaint();
  }
}
